package com.usercon.userconter.controller;

import com.usercon.userconter.Utils.MD5Util;
import com.usercon.userconter.Utils.ResultAll.Result;
import com.usercon.userconter.Utils.ResultAll.ResultUtils;
import com.usercon.userconter.Utils.ResusltTest.ResultUtilsTest;
import com.usercon.userconter.Utils.ResusltTest.ResultTest;
import com.usercon.userconter.pojo.User;
import com.usercon.userconter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin(value = "*",maxAge = 3600)
@RestController
@RequestMapping("/api")
public class UserController {

    private UserService userService;

    private ResultUtilsTest result;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //查询所有用户
    @GetMapping
    public Result findAll(){
        List<User> findAll = userService.findAll();
        return ResultUtils.success(findAll);
    }

    //根据ID查找账户信息，如果账户状态码为20则返回，否则返回查询失败提示。
    @GetMapping("/findById")
    public ResultTest findById(@RequestParam("id") int id){
        Integer status = userService.userStatus(id);
        if(status == 20){
//            return ResultUtils.success(userService.findById(id)).setMsg("查询成功");
            return result.define(userService.findById(id),result.setMeta(200,"查询成功"));
        }else{
//            return ResultUtils.success("查询失败","账户状态失效");

            return result.define("查询失败",result.setMeta(500,"用户状态已注销"));
        }
    }

    //用户增加
    @PostMapping
    public Result addUser(@RequestParam("id") int id,@RequestParam("username") String username,@RequestParam("password") String password){
        int addUser = userService.addUser(id,username,password);
        System.out.println(addUser);
        if(addUser > 0){
            return ResultUtils.success(addUser).setCode(200).setMsg("成功");
        }else{
            return ResultUtils.success(addUser).setCode(500);
        }

    }

    //用户信息更新
    @PutMapping
    public Result updateUser(User user){
        int updateUser = userService.updateUser(user);
        if(updateUser > 0){
            return ResultUtils.success(updateUser).setCode(200).setMsg("修改成功");
        }else{
            return ResultUtils.success(updateUser).setCode(500).setMsg("修改Error");
        }
    }

    @DeleteMapping
    public Integer deleteUser(@RequestParam("id") int id){
        return userService.deleteUser(id);
    }

    // status 字段查询
    @GetMapping("/findStatus")
    public Result findStatus(int id){
        Integer integer = userService.userStatus(id);
        System.out.println(integer);
        return ResultUtils.success(integer).setMsg("状态查询成功").setCode(200);
    }

    @GetMapping("/findPass")
    public Result findPass(@RequestParam("id") int id){
        System.out.println(userService.getPass(id));
        return ResultUtils.success(userService.getPass(id)).setCode(200).setMsg("返回密码");
    }

    //重设密码
    @PutMapping("/updatePass")
    public Result updatePass(int id, String password,String NewPassword){

        String oldPass = password;
        String dPass = userService.getPass(id);

        if(oldPass.equals(dPass)){
            Integer upDataPass = userService.updatePass(id,NewPassword);
            return ResultUtils.success(upDataPass).setCode(200).setMsg("密码修改成功");
        }else{
            return ResultUtils.success("原密码输入错误");
        }
    }


    //登录接口
    @PostMapping("/login")
    public Result login(String username,String password){
        User login = userService.login(username,password);
        if(login != null){
            return ResultUtils.success(login).setMsg("登录成功").setCode(200);
        }else{
            String ParsePass = MD5Util.MD5Encode(password,"UTF-8");
            User NLogin = userService.login(username,ParsePass);
            if(NLogin != null){
                return ResultUtils.success(NLogin).setMsg("登录成功").setCode(200);
            }else{
                return ResultUtils.success("登录错误").setCode(500).setMsg("账号或密码错误");
            }
        }
    }

}
